package cn.kgc.springboot06.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author: mosin
 * @version: v1.0  2023/1/5
 * redis数据库操作工具类
 */
@Component
public class RedisUtil {

    @Autowired
    private  StringRedisTemplate srt;

    //添加String类型的值
    public void setValue(String key,String value){
        srt.opsForValue().set(key, value);
    }

    // 添加string类型值同时设置生命周期
    public  void setValue(String key, String value, long timeOut, TimeUnit timeUnit){
        srt.opsForValue().set(key, value, timeOut, timeUnit);
    }

    // 添加string类型值 避免覆盖
    public   void setIfAbsentValue(String key, String value){
        srt.opsForValue().setIfAbsent(key, value);
    }

    // 更新string类型值
    public  void setIfPresentValue(String key, String value){
        srt.opsForValue().setIfPresent(key, value);
    }

    //判定redis中是否存在某个key
    public   Boolean hasKey(String key){
        Boolean aBoolean = srt.hasKey(key);
        return aBoolean;
    }

    // 获取指定key对应的值
    public  String getValue(String key){
        String s = srt.opsForValue().get(key);
        return s;
    }

    // 删除指定key对应的值
    public  Boolean deleteByKey(String key){
        Boolean aBoolean = srt.delete(key);
        return aBoolean;
    }


}
